package org.example;

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpRequest.BodyPublisher;
import java.net.http.HttpResponse;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.concurrent.CompletableFuture;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLParameters;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

public class Utils {

  public static CompletableFuture<HttpResponse<String>> getFromUrl(String url)
      throws NoSuchAlgorithmException, KeyManagementException {

    System.out.println("start to get:" + url + " " + LocalDateTime.now());
    //https://www.baeldung.com/java-httpclient-ssl
    System.setProperty("jdk.internal.httpclient.disableHostnameVerification", "true");//取消主机名验证
    HttpClient httpClient = HttpClient.newBuilder()
        .connectTimeout(Duration.ofSeconds(10))
        .build();
    HttpRequest httpRequest = HttpRequest.newBuilder()
        .version(HttpClient.Version.HTTP_2)
        .uri(URI.create(url))
        .header("Content-Type", "text/json")
        .timeout(Duration.ofSeconds(10))
//        .POST(HttpRequest.BodyPublishers.ofString("{}"))
        .GET()
        .build();
    HttpResponse.BodyHandler<String> bh = HttpResponse.BodyHandlers.ofString();
    return httpClient.sendAsync(
        httpRequest, bh);

  }
}
